<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cookie;


class Wlog extends BaseController
{
    protected $middleware = [
        //'app\middleware\Check' => ['except' => ['login']],
        'app\middleware\Check',
    ];

    public function index(){
        $search = $this->request->param('search');

        $obj = Db::table('wlog')->join('wlogcate',"wlog.cateid=wlogcate.id")->field('wlog.id,wlog.title,wlog.content,wlog.cateid,wlog.datetime,wlog.status,wlog.updatetime,wlogcate.cate_name');
        if(!empty($search)){
          $obj = $obj->whereLike('cate_name',"%$search%")->order('sort')->paginate(2);
        }else{
            $select = $obj->where('is_del',0)->paginate(2);
        }
        return view('index',['select'=>$select]);
    }

    public function recovery(){
        $search = $this->request->param('search');

        $obj = Db::table('wlog')->join('wlogcate',"wlog.cateid=wlogcate.id")->field('wlog.id,wlog.title,wlog.content,wlog.cateid,wlog.datetime,wlog.status,wlog.updatetime,wlogcate.cate_name');
        if(!empty($search)){
            $obj = $obj->whereLike('cate_name',"%$search%")->order('sort')->paginate(2);
        }else{
            $select = $obj->where('is_del',1)->paginate(2);
        }
        return view('recovery',['select'=>$select]);
    }

    public function reduction(){
        $id = $this->request->param('id');
        $res = Db::table('wlog')->save(['id' => $id,'is_del'=>0]);
        if($res){
            return redirect('/wlog/index');
        }else{
            return "分类删除失败";die();
        }
    }

    public function rdel(){
        $id = $this->request->param('id');
        $res = Db::table('wlog')->delete($id);  //调用博客删除方法进行数据删除
        if($res){
            return redirect('/wlog/recovery');
        }else{
            return "数据删除失败";die();
        }
    }
    public function add(){
        $list = Db::table('wlogcate')->select();

        if($this->request->isPost()){
            $_POST['updatetime']=date('Y-m-d H:i:s');
            $_POST['datetime']=date('Y-m-d H:i:s');
            $res = Db::name('wlog')->save($_POST); //提交到博客新增方法进行入库
            if($res){
                return redirect('index');
            }else{
                return "博客添加失败";die();
            }
        }
        return view('add',['list'=>$list]);  //跳转新增表单
    }

    public function del(){
        $id = $this->request->param('id');
        $res = Db::table('wlog')->save(['id' => $id,'is_del'=>1]);  //调用博客删除方法进行数据删除
        if($res){
            return redirect('/wlog/index');
        }else{
            return "分类删除失败";die();
        }
    }

    public function update(){
        $id = $this->request->param('id');
        $info = Db::table('wlog')->find($id);
        $list = Db::table('wlogcate')->select();
        if ($this->request->isPost()) {
            $_POST['id']= $id;
            $res = Db::name('wlog')->save($_POST); //提交到博客分类修改方法进行数据修改
            //提交到博客修改方法进行数据修改
            if ($res) {
                return redirect('/wlog/index');
            } else {
                return "修改失败";
                die();
            }
        }
        return view('add',['list'=>$list,'info' =>$info]);
    }

    public function status(){
        $id = $this->request->param('id');

        $res = Db::table('wlog')->save(['id'=>$id,'status'=>1]);
        if($res){
            return redirect('/wlog/index');
        }else{
            return '发布失败';die();
        }
    }
}

